<?php

namespace App\Service\Model;

use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class DayTotal extends \Eloquent
{

    
    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * The database used by the model.
     *
     * @var string
     */
    protected $table = 'day_total';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [''];
    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [''];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [];

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
    }
    
    public static function incr($agency_id,$column , $number = 1)
    {
        $data = self::where('agency_id', $agency_id)->where('date', Carbon::now()->toDateString())->first();
        if(empty($data)){
            $agency  = Agency::find($agency_id);
            $data    = self::create([
                'date'          => Carbon::now()->toDateString(),
                'agency_id'     => $agency_id,
                'agency_name'   => $agency->name,
                'invite_code'   => $agency->invite_code,
                'agency_level'  => $agency->level,
                'ratio'         => $agency->ratio,
            ]);
        }
        $data->{$column} += $number;
        $data->save();
    }
}
